package zhang.jdbc.graph;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import zhang.utils.JDBCUtils;
import zhang.utils.User;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField tid;
	private JPasswordField tpassword;

	/**
	 * role
	 * 0:教师
	 * 1:学生
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 423, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("民院教务管理");
		lblNewLabel.setFont(new Font("黑体", Font.PLAIN, 32));
		lblNewLabel.setBounds(99, 10, 235, 51);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("用户名：");
		lblNewLabel_1.setBounds(64, 75, 58, 15);
		contentPane.add(lblNewLabel_1);
		JButton login = new JButton("登录");
		JLabel lblNewLabel_2 = new JLabel("密码：");
		lblNewLabel_2.setBounds(64, 120, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		tid = new JTextField();
		tid.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
//				if(tid.getText().equals("")) {
//					JOptionPane.showMessageDialog(null, "账号不能为空！");
//					login.setEnabled(false);
//				}
			}
		});
		tid.setBounds(132, 72, 131, 21);
		contentPane.add(tid);
		tid.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("注册");
		lblNewLabel_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new Regisn();
			}
		});
		lblNewLabel_3.setBounds(276, 195, 58, 15);
		contentPane.add(lblNewLabel_3);
		JRadioButton rads = new JRadioButton("学生");
		JRadioButton radt = new JRadioButton("教师");
		rads.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				radt.setSelected(false);
			}
		});
		
		radt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				rads.setSelected(false);
			}
		});
		radt.setBounds(105, 191, 63, 23);
		contentPane.add(radt);
		
		
		rads.setSelected(true);
		rads.setBounds(170, 191, 63, 23);
		contentPane.add(rads);
		
		
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int flag = radt.isSelected()==true?0:1;
				if(tid.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "账号不能为空！");
				}else {
				JDBCUtils jdbc = new JDBCUtils();
				int id = Integer.parseInt(tid.getText());
				User user=null;
				user = jdbc.selectOne(User.class, id);
				if(user.getName()==null) {
					JOptionPane.showMessageDialog(null, "账号错误！");
					
				}else if(user.getPassword().equals(tpassword.getText())&&user.getRole()==flag) {
					JOptionPane.showMessageDialog(null, "成功");
					new JdbcGrap(user.getRole());
				Login.this.setVisible(false);
				}else {
					JOptionPane.showMessageDialog(null, "密码错误！");
				}
			}
			}
		});
		login.setBounds(136, 230, 97, 23);
		contentPane.add(login);
		
		tpassword = new JPasswordField();
		tpassword.setBounds(132, 118, 131, 21);
		contentPane.add(tpassword);
	}
}
